package app.web.visual.tags;

import app.common.dao.Leo_tagBean;
import app.common.dao.Leo_tag_typ_relaBean;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.Basic;
import cn.lily.com.lang.BusinessDate;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.sql.TSqlGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;
/**
 * 标签主类
 * @author JiaJ
 *
 */
public class TagMain extends BusinessPage { 

	 public void page_onLoad() throws YaconException {

		 String tag_typ_id = this.getAttr("tag_typ_id");
		 
		 TSqlGrid tagMainGrid = sqlGrid("tagMainGrid");
		 tagMainGrid.setDictTable("LEO_TAG");
		 String sqlString = "select TAG_ID,TAG_NAM,to_char(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss') CREATE_TIME,CREATE_OPER "
					 	  + "from ${schema}.LEO_TAG "
					 	  + "where TAG_ID in (select TAG_ID from LEO_TAG_TYP_RELA where TAG_TYP_ID='"+tag_typ_id+"') "
					 	  + "order by CREATE_TIME DESC";
		 tagMainGrid.setCommSqlStr(sqlString);
		 tagMainGrid.setEdit(true);
		 tagMainGrid.setExcuteMethod("save");
		 tagMainGrid.setRefreshMethod("refresh");

		 tagMainGrid.createFieldTitle("TAG_ID").setFieldVisible(false);
		 tagMainGrid.createFieldTitle("TAG_NAM");
		 tagMainGrid.createFieldTitle("CREATE_TIME");
		 tagMainGrid.createFieldTitle("CREATE_OPER").setFieldVisible(false);

		 TButtonPilot tagMainPilot = ButtonPilot("tagMainPilot");
		 tagMainPilot.setPilotIco("/widget/buttonPilot.gif");
		 tagMainPilot.setPilotText("标签信息表信息");

		 TButton addButton = new TButton("add");
		 TButton delButton = new TButton("delete");
		 TButton saveButton = new TButton("save");
		 tagMainPilot.addButtons(addButton);
		 tagMainPilot.addButtons(delButton);
		 tagMainPilot.addButtons(new TButton("cancle"));
		 tagMainPilot.addButtons(saveButton);
		 
		 //按钮添加
		 TButton tagReset = Button("tagReset");
		 tagReset.setIcoPath("glyphicon glyphicon-link");
		 tagReset.setLabel("绑定标签");
		 tagReset.setViewClick("tagReset_onclick");
		 tagMainPilot.addButtons(tagReset);

		 tagMainGrid.setButtonPilot(tagMainPilot);
		 
		 tagMainGrid.setTopButtonType("all");
		 
		 getPage().addElement(tagMainGrid);

	 }

	 public void refresh() throws YaconException {
		 
		 String tag_typ_id = this.getViewTable().getAttributeValue("tag_typ_id");

		 TSqlGrid tagMainGrid = sqlGrid("tagMainGrid");
		 String sqlString = "select TAG_ID,TAG_NAM,to_char(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss') CREATE_TIME,CREATE_OPER,'"+tag_typ_id+"' as TAG_TYP_ID "
					 	  + "from ${schema}.LEO_TAG "
					 	  + "where TAG_ID in (select TAG_ID from LEO_TAG_TYP_RELA where TAG_TYP_ID='"+tag_typ_id+"') "
					 	  + "order by CREATE_TIME DESC";
		 tagMainGrid.setCommSqlStr(sqlString);
		 this.setSelfMessage(tagMainGrid.getDataElement());

	 }

	 public void save() throws YaconException {

		 String tag_typ_id = this.getViewTable().getAttributeValue("tag_typ_id");
		 
		 Leo_tagBean tagMainBean = new Leo_tagBean();
		 Leo_tag_typ_relaBean relaBean = new Leo_tag_typ_relaBean();
		 
		 while (getViewTable().next()) {
			 tagMainBean.setViewRecord(getViewTable().getRecord());

			 if (getViewTable().getRecord().getRecordType() == 1) {
				 
				 this.dataExistCommon(tagMainBean, "TAG_NAM", "该标签已经存在！");
				 
				 String tag_id = this.getDBSequence().getBussSeq("leo_tag_id", 8);
				 tagMainBean.setTag_id(tag_id);
				 tagMainBean.setCreate_oper(this.getOperator().getOperId());
				 tagMainBean.setCreate_time(BusinessDate.getTodaytime());
				 
				 tagMainBean.insert();
				 
				 relaBean.setTag_id(tag_id);
				 relaBean.setTag_typ_id(tag_typ_id);
				 relaBean.insert();
				 
			 }

			 else if (getViewTable().getRecord().getRecordType() == 3) {			
				 
				 relaBean.setTag_id(this.getViewTable().getString("TAG_ID"));
				 relaBean.setTag_typ_id(tag_typ_id);
				 relaBean.deleteByWhere("where TAG_ID='"+this.getViewTable().getString("TAG_ID")+"' and TAG_TYP_ID='"+tag_typ_id+"'");
			 }

		 }
		 this.setSuccess("数据操作成功！");

	 }
	 
	 public void auth() throws YaconException {
		 
		 Leo_tag_typ_relaBean relaBean = new Leo_tag_typ_relaBean();
		 String typeID = "";
		 String tags = "";
		 
		 if(getViewTable().next()){
			 typeID = getViewTable().getString("typeID");
			 tags = getViewTable().getString("tags");
			
			 //逗号拆分
			 String[] tagss = Basic.split(tags, ",","，");
			 
			 for(String tag:tagss){
				 relaBean.setTag_id(tag);
				 relaBean.setTag_typ_id(typeID);
				 relaBean.insert();
			 }
			 
		 }
		 
		 this.setSuccess("绑定成功!");
		 
	}

}
